package com.buaa.wangrui.client.panel;

import com.buaa.wangrui.client.DatabaseService;
import com.buaa.wangrui.client.DatabaseServiceAsync;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.ToolbarItem;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.core.*;



	public class LoginPanel extends Window {
		 
		 public Toolbar toolbar;
		 public TextField txtName;
		 public TextField txtPsw;
		 final MainPanel mainPanel = new MainPanel();

		 
		 public LoginPanel () {
		 
		 this.setTitle("身份验证");
		 this.setWidth(475);
		 this.setDraggable(false);
		 this.setClosable(false);
		 this.setIconCls("eastPanel_1-icon");
		 
		 Image logo = new Image();
		 logo.setUrl("images/logo2.jpg");
		 logo.setVisible(true);
		 
		 
		 toolbar = new Toolbar();
		 toolbar.setWidth(475);
		 
		 txtName = new TextField();
		 txtName.setValue("");
		 
		 txtPsw = new TextField();
		 txtPsw.setPassword(true);
		 
		 ToolbarButton bt1 = new ToolbarButton("登 陆");
		 
		 
		 Image image = new Image();
		 image.setUrl("images/user.gif");
		 
		 
		 
		 toolbar.addText("用户名");
		 toolbar.addSpacer();
		 toolbar.addItem(new ToolbarItem(new Image("images/user.gif").getElement()));
		 toolbar.addSpacer();
		 toolbar.addField(txtName);
		 toolbar.addSpacer();
		 
		 
		 toolbar.addText("密码");
		 toolbar.addSpacer();
		 toolbar.addItem(new ToolbarItem(new Image("images/mm.gif").getElement()));
		 toolbar.addSpacer();
		 toolbar.addField(txtPsw);
		 toolbar.addSpacer();
		 toolbar.addButton(bt1);
		 toolbar.addSpacer();
		 
		 bt1.addListener(new ButtonListenerAdapter(){
			
			 public void onClick(Button Button,EventObject e){
				 
				 String username = txtName.getText();
				 
				 String password = txtPsw.getText();
				 
				 DatabaseServiceAsync loginService = DatabaseService.Util.getInstance();
				 
				 AsyncCallback cb_login = new AsyncCallback(){

					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					public void onSuccess(Object result) {
						// TODO Auto-generated method stub
						Boolean ok = (Boolean)result;
						if(ok.booleanValue()){
							
							RootPanel.get().clear();
							
							close();

							RootPanel.get().add(mainPanel);
							
							applyEffect("Pulsate", mainPanel.welcLabel
									.getElement());// 不用再加RootPanel.get().add(welcLabel)可直接显示
							
						}
					}
					 
				 };
				 
				 loginService.login(username, password, cb_login);
				 
			 }
		 });
	                 
		                 this.add(logo);
		                 this.add(toolbar);
		                 
 }

		 
	private native void applyEffect(String effect, Element element)/*-{
	
	var ne = $wnd._nativeExtensions;
	
	$wnd._nativeExtensions = false;

    $wnd.Effect[effect](element);
    
    $wnd.__nativeExtensions = ne;
}-*/;
}